//
//  HappyChildrenPublicViewController.m
//  HappyChildren
//
//  Created by WillyZhao on 2016/12/16.
//  Copyright © 2016年 WillyZhao. All rights reserved.
//

#import "HappyChildrenPublicViewController.h"


@interface HappyChildrenPublicViewController () {
}


@end

NSInteger *ff;              //用来存放地址的指针

@implementation HappyChildrenPublicViewController

@synthesize tabBarView =  tabBarView;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置颜色
    self.view.backgroundColor = [UIColor whiteColor];
    
    // Do any additional setup after loading the view.
}

#pragma mark setter
-(void)setDataArray:(NSArray *)dataArray {
    _dataArray = dataArray;
    
    NSInteger aCount = _dataArray.count;
    
    if (aCount>0) {
        free(ff);           //先释放再开辟
        ff=(NSInteger *)malloc(sizeof(NSInteger)*aCount);
        for (NSInteger i = 0; i<aCount; i++) {
            objc_setAssociatedObject(self, &ff[i], [[NSClassFromString(_dataArray[i][@"viewcontroller"]) alloc]init], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        }
    }
    
    //首页
    HCParentMenuViewController *homeVC = objc_getAssociatedObject(self, &ff[0]);
    [self addChildViewController:homeVC];
    [homeVC didMoveToParentViewController:self];
    [self.view addSubview:homeVC.view];
    self.currentVC = homeVC;
    self.currentVC.isSelectedVC = YES;          //设置为当前选中
    
    //构建tabbarview
    tabBarView = [[HappyChildrenTabBarController alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-TabBarHeight, self.view.frame.size.width, TabBarHeight)];
    tabBarView.tabsArray = _dataArray;
    
    tabBarView.delegate = (id<TabBarViewDelegate>)self;
    UIView *lineView= [[UIView alloc]initWithFrame:CGRectMake(0, 0, YHGetSizeWidth(tabBarView), 0.5)];
    lineView.backgroundColor=YBKRGBCOLOR(244, 244, 244, 1.0);
    [tabBarView addSubview:lineView];
    [self.view addSubview:tabBarView];
    
    NSLog(@"当前屏幕宽度:%@",NSStringFromCGSize([UIScreen mainScreen].currentMode.size));
    NSLog(@"屏幕宽度：%@",NSStringFromCGSize([UIScreen mainScreen].bounds.size));
    
}

-(void)selectedIndexPath:(NSInteger)index {
    [tabBarView selectedIndexPath:index];
}

-(void)tabBarSelectedView:(HappyChildrenTabBarController *)tabBarView indePath:(NSInteger)index{
    [self didClickedHeadButtonAction:index];
}

#pragma mark 切换按钮
-(void)didClickedHeadButtonAction:(NSInteger)indexPath {
    for (NSInteger i = 0; i<_dataArray.count; i++) {
        NSInteger index = [_dataArray[i][@"MenuID"] integerValue];
        if (indexPath == index) {
            UIViewController *vc = objc_getAssociatedObject(self, &ff[i]);
            [self replaceController:self.currentVC newController:(HCParentMenuViewController *)vc];       //上一层已经除去重复的了，所以直接调用
        }
    }
}

#pragma mark 切换各个标签的内容
-(void)replaceController:(HCParentMenuViewController *)oldController newController:(HCParentMenuViewController *)newController {
    /**
     *            着重介绍一下它
     *  transitionFromViewController:toViewController:duration:options:animations:completion:
     *  fromViewController      当前显示在父视图控制器中的子视图控制器
     *  toViewController        将要显示的姿势图控制器
     *  duration                动画时间(这个属性,old friend 了 O(∩_∩)O)
     *  options                 动画效果(渐变,从下往上等等,具体查看API)
     *  animations              转换过程中得动画
     *  completion              转换完成
     */
    
    [self addChildViewController:newController];
    [self transitionFromViewController:oldController toViewController:newController duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
        
        if (finished) {
            [newController didMoveToParentViewController:self];
            [oldController willMoveToParentViewController:nil];
            [oldController removeFromParentViewController];
            self.currentVC.isSelectedVC = NO;
            self.currentVC = newController;
            self.currentVC.isSelectedVC = YES;      //设置新的选中
        }else{
            self.currentVC = oldController;
        }
        
    }];
    
}

#pragma mark 释放
-(void)dealloc {
    free(ff);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
